package ua.dlab.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel pnlMain = new JPanel(new GridBagLayout());
	private JPanel pnlContent = new JPanel(new BorderLayout());
	private JButton btnAction = new JButton();
	private JButton btnNext = new JButton("Next");
	private JButton btnPrevious = new JButton("Before");
	private Step step = new Step1();

	public View() {
		setTitle("СЛАР. Метод простої ітерації");
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		pnlContent.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY));
		btnAction.setVisible(false);
		
		pnlMain.add(pnlContent, 
				new GridBagConstraints(0, 0, 4, 1, 1, 1, GridBagConstraints.NORTHWEST,
						GridBagConstraints.BOTH, new Insets(0, 0, 6, 0), 0, 0));
		pnlMain.add(new JPanel(), 
				new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.WEST,
						GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		pnlMain.add(btnAction, 
				new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
						GridBagConstraints.NONE, new Insets(6, 12, 12, 6), 0, 0));
		pnlMain.add(btnPrevious, 
				new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
						GridBagConstraints.NONE, new Insets(6, 0, 12, 6), 0, 0));
		pnlMain.add(btnNext, 
				new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
						GridBagConstraints.NONE, new Insets(6, 0, 12, 12), 0, 0));
		
		btnNext.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				step = step.nextStep();
				updateView();
			}
		});
		
		btnPrevious.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				step = step.previousStep();
				updateView();
			}
		});
		
		add(pnlMain);
		updateView();
		
		setSize(new Dimension(750, 400));
		setVisible(true);
	}
	
	private void updateView() {
		pnlContent.removeAll();
		pnlContent.add(step.getMainArea(), BorderLayout.CENTER);
		pnlContent.validate();
		pnlContent.repaint();
		
		if (step.hashNext()) {
			btnNext.setEnabled(true);
		} else {
			btnNext.setEnabled(false);
		}
		
		if (step.hashPrevious()) {
			btnPrevious.setEnabled(true);
		} else {
			btnPrevious.setEnabled(false);
		}
		
		Action action = step.getAction();
		if (action != null) {
			btnAction.setAction(action);
			btnAction.setVisible(true);
		} else {
			btnAction.setAction(null);
			btnAction.setVisible(false);
		}
	}
	
	@Override
	public void setVisible(boolean b) {
		if (b) {
			moveInPosition();
		}

		super.setVisible(b);
	}


	public void moveInPosition() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 2 - getWidth() / 2, screenSize.height / 2 - getHeight() / 2);
	}
}
